//index.js
var WxParse = require('../../../wxParse/wxParse.js');
const app = getApp()
Page({
  data: {
    id:'',
    goods:[],
    details:'',
    lo:'https://xiaochengxu.printeasy.top',
    guigeId:'',//规格id
    showModal:false,
    idx:'',
    ImgUrl:'',
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '商品详情', //导航栏 中间的标题
    },
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.height * 2 + 20,

    indicatorDots: true, // 是否显示面板指示点
    autoplay: true, // 是否自动切换
    interval: 5000, // 自动切换时间间隔
    duration: 500, // 滑动动画时长
    circular: true, // 是否采用衔接滑动
    /*自定义轮播图 配置*/
    slider: [{
      id: '0',
      linkUrl: 'pages/index/index',
      picUrl: 'http://img1.gtimg.com/rushidao/pics/hv1/20/108/1744/113431160.jpg'
    },
    {
      id: '1',
      linkUrl: 'pages/index/index',
      picUrl: 'http://g.hiphotos.baidu.com/zhidao/pic/item/ac4bd11373f08202b4a9a53a4bfbfbedab641bff.jpg'
    },
    {
      id: '2',
      linkUrl: 'pages/index/index',
      picUrl: 'http://file02.16sucai.com/d/file/2014/0829/b871e1addf5f8e96f3b390ece2b2da0d.jpg'
    }]
  },
  // 外面的弹窗
  btn: function () {
    this.setData({
      showModal: true
    })
  },

  // 禁止屏幕滚动
  preventTouchMove: function () {
  },

  // 弹出层里面的弹窗
  ok: function () {
    this.setData({
      showModal: false
    })
  },
  onLoad(options) {
    let id = options.id;
    console.log(id)
    let that = this;
    let url = app.globalData.URL+'goods/commodity/'+id;
        let data = {};
        app.wxRequest('GET', url, data, (res) => {
          let goods = res
          let details = goods.data.details
          let ImgUrl = res.data.carouselUrl
          ImgUrl = ImgUrl.replace(/\"/g, "")
          ImgUrl = ImgUrl.split(";")
          console.log(goods,ImgUrl)
          that.setData({
            id:id,
            goods:goods,
            ImgUrl:ImgUrl
            // details:details
          })
          WxParse.wxParse('details', 'html',details, that, 5);
        }, (err) => {
          console.log(err.errMsg)
        })
  },
  // 选择规格
  selectGe(e){
    console.log(e)
    this.setData({
      guigeId:e.currentTarget.dataset.id,
      idx:e.currentTarget.dataset.index
    })
  },
  // 添加购物车
  addshopcar(){
    let that = this;
    let url = app.globalData.URL+'goods/cart?commodityId='+that.data.id+'&count=1'+'&modelId='+that.data.guigeId;
    if(that.data.guigeId==''){
      wx.showModal({
        title: '请选择规格',
      })
    }else{
        let data = {};
        app.wxRequest('POST', url, data, (res) => {
          if(res.code!=200){
            wx.showModal({
              title: res.msg,
            })
          }else{
            wx.showModal({
              title: '添加商品成功',
              success(){
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            })
          }
        }, (err) => {
          console.log(err.errMsg)
        })
      }
  },
  // 提交
  subim:function(e){
    let that = this;
    let id =that.data.id;
    if(that.data.guigeId==''){
      wx.showModal({
        title: '请选择规格',
      })
    }else{
      let modelId = that.data.guigeId;
    console.log(id)
    wx.navigateTo({
      url: '/pages/myorder/confirm_order/confirm_order?id='+id+'&modelId='+modelId+'&idx='+that.data.idx,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    }
    
  }

})
